#include <cstdio>
using namespace std;
/*
 * @lc app=leetcode.cn id=143 lang=cpp
 *
 * [143] 重排链表
 */
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if (!head) return;
        ListNode *cur = head, *pre = nullptr;
        while (cur->next) {
            pre = cur; cur = cur->next;
        }
        if (!pre) return;
        pre->next = nullptr;
        if (!head) return;
        ListNode *next = head->next;
        head->next = cur;
        head->next->next = next;
        reorderList(next);
    }
};
// @lc code=end

